﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AresWebService;
using AresWebService.Ares;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AresWebService.Tests
{
	[TestClass]
	public class AresServiceTest
	{
		[TestMethod]
		public void Test()
		{
			Assert.IsTrue(AresServiceResponseError.None == AresServiceResponseError.None, "AresServiceResponseError.None != AresServiceResponseError.None ???");
			Assert.IsTrue(AresServiceResponseError.None != AresServiceResponseError.InvalidIco, "AresServiceResponseError.None == AresServiceResponseError.InvalidIco ???");
		}
		[TestMethod]
		public async Task FindIcoTest()
		{
			var a = new AresService();
			//getmore, s.r.o.
			var ret = await a.FindByIcoAsync("25577581");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.AreEqual(z.Ico, "25577581");
			Assert.AreEqual(z.CompanyName, "GETMORE, s.r.o.", false);
			Assert.IsTrue(z.IsVatRegistered, "IsVatRegistered not true! Please check ARES.");
			Assert.IsFalse(z.IsProblematicSubject);

			//problematicky subjekt...AFD - group, s.r.o.
			ret = await a.FindByIcoAsync("28328736");
			Assert.IsNotNull(ret);
			z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.AreEqual(z.Ico, "28328736");
			Assert.IsFalse(z.IsVatRegistered, "IsVatRegistered not false! Please check ARES.");
			Assert.IsTrue(z.IsProblematicSubject);
		}

		[TestMethod]
		public async Task FindIcoTestWrongIco()
		{
			var a = new AresService();
			//neplatne ico
			var ret = await a.FindByIcoAsync("00000000");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsFalse(ret.Succesfull);
			Assert.IsNull(z);
		}
		[TestMethod]
		public async Task FindIcoTestNotFoundIco()
		{
			var a = new AresService();
			//neplatne ico
			var ret = await a.FindByIcoAsync("44996306");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsFalse(ret.Succesfull);
			Assert.IsNull(z);
		}
		[TestMethod]
		public async Task FindIcoTestProbablyCorrectIco()
		{
			var a = new AresService();
			//neplatne ico
			var ret = await a.FindByIcoAsync("416 93 426");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNotNull(z);
		}

		[TestMethod]
		public async Task FindIcoTestRzp()
		{
			var a = new AresService();
			//Milan Borecký..prerusena zivnost...
			var ret = await a.FindByIcoAsync("75959674");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNotNull(z.DateTerminated, "DateTerminated is null! Please check ARES.");
			Assert.IsTrue(z.DateTerminated == new DateTime(2013, 10, 30));
		}
		[TestMethod]
		public async Task FindIcoTestRzp2()
		{
			var a = new AresService();
			//Jaroslav Dokulil..vice zivnosti, zacatek kolem 1992, pak znovu 2012...
			var ret = await a.FindByIcoAsync("46184414");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNull(z.DateTerminated, "DateTerminated is not null! Please check ARES.");
			Assert.IsNotNull(z.DateCreated, "Date Created null! Please check ARES.");
			Assert.IsTrue(z.DateCreated == new DateTime(1992, 12, 7), "Date Created invalid! Please check ARES.");
		}
		[TestMethod]
		public async Task FindIcoTestRzp3()
		{
			var a = new AresService();
			//milan matejka..
			var ret = await a.FindByIcoAsync("70451583");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNotNull(z.DateTerminated, "DateTerminated is null! Please check ARES.");
			Assert.IsTrue(z.DateTerminated == new DateTime(2011, 12, 14));
			Assert.IsNotNull(z.DateCreated, "Date Created null! Please check ARES.");
			Assert.IsTrue(z.DateCreated == new DateTime(2004, 1, 1), "Date Created invalid! Please check ARES.");
		}
		[TestMethod]
		public async Task FindIcoTestRzp4()
		{
			var a = new AresService();
			//JAROSLAV ŠINDELÁŘ..
			var ret = await a.FindByIcoAsync("87781735");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNotNull(z.DateTerminated, "DateTerminated is null! Please check ARES.");
			Assert.IsTrue(z.DateTerminated == new DateTime(2013, 11, 25));
			Assert.IsNotNull(z.DateCreated, "Date Created null! Please check ARES.");
			Assert.IsTrue(z.DateCreated == new DateTime(2011, 4, 13), "Date Created invalid! Please check ARES.");
		}
		[TestMethod]
		public async Task FindIcoTestRzp5()
		{
			var a = new AresService();
			//JAN ANDRLE..
			var ret = await a.FindByIcoAsync("72045825");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNotNull(z.DateTerminated, "DateTerminated is null! Please check ARES.");
			Assert.IsTrue(z.DateTerminated == new DateTime(2013, 11, 25));
			Assert.IsNotNull(z.DateCreated, "Date Created null! Please check ARES.");
			Assert.IsTrue(z.DateCreated == new DateTime(2009, 8, 6), "Date Created invalid! Please check ARES.");
		}
		[TestMethod]
		public async Task FindIcoTestRzp6()
		{
			var a = new AresService();
			//Misijní rodina Donum Dei..
			var ret = await a.FindByIcoAsync("66740061");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNull(z.DateTerminated, "DateTerminated is not null! Please check ARES.");
			Assert.IsNotNull(z.DateCreated, "Date Created null! Please check ARES.");
			Assert.IsTrue(z.DateCreated == new DateTime(1997, 05, 20), "Date Created invalid! Please check ARES.");
		}
		
		[TestMethod]
		public async Task FindIcoTestEzp()
		{
			//EZP je zdroj, ktery neni uveden v normalni deklaraci... nutno doplnit rucne do enum zdroj_type
			var a = new AresService();
			//Sýkorová Bláhová Michaela..je EZP a zaroven neni v RZP = nelze se dotazat na RZP...
			var ret = await a.FindByIcoAsync("70961948"); //http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_szr.cgi?ico=70961948&jazyk=cz&xml=1
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNull(z.DateTerminated, "DateTerminated is not null! Please check ARES.");
			//http://adisreg.mfcr.cz/cgi-bin/adis/idph/int_dp_prij.cgi?id=1&pocet=1&fu=&OK=+Search+&ZPRAC=RDPHI1&dic=7952074526
			Assert.IsTrue(z.IsVatRegistered, "IsVatRegistered not true! Please check ARES.");
			Assert.IsNotNull(z.DateCreated, "Date Created null! Please check ARES.");
			Assert.IsTrue(z.DateCreated == new DateTime(2006, 7, 19), "Date Created not correct! Please check ARES."); //nejake divne, viz http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_res.cgi?ico=70961948&jazyk=cz&xml=1
		}


		[TestMethod]
		public async Task FindIcoTestMultipleReg()
		{
			var a = new AresService();
			//milan matejka..
			var ret = await a.FindByIcoAsync("12992631");
			Assert.IsNotNull(ret);
			var z = ret.Response;
			Assert.IsTrue(ret.Succesfull, "ret.Succesfull is not true!");
			Assert.IsNull(z.DateTerminated, "DateTerminated is not null! Please check ARES.");
			Assert.IsNotNull(z.DateCreated, "Date Created null! Please check ARES.");
			Assert.IsTrue(z.DateCreated == new DateTime(1990, 5, 1), "Date Created invalid! Please check ARES.");
		}
		[TestMethod]
		public void CheckValidIcoTest()
		{
			var ico = "25596641";
			var ret = AresService.CheckValidIco(ref ico);
			Assert.IsTrue(ret);
			ico = "416 93 426";
			ret = AresService.CheckValidIco(ref ico);
			Assert.IsTrue(ret);
			Assert.IsTrue(ico == "41693426");
			ico = "  69 6 639 63 ";
			ret = AresService.CheckValidIco(ref ico);
			Assert.IsTrue(ret);
			ico = "  9 6 639 63 ";
			ret = AresService.CheckValidIco(ref ico);
			Assert.IsFalse(ret);
			ico = null;
			ret = AresService.CheckValidIco(ref ico);
			Assert.IsFalse(ret);
			ico = "";
			ret = AresService.CheckValidIco(ref ico);
			Assert.IsFalse(ret);
		}
		[TestMethod]
		public void CheckSubjectInfo()
		{
			var a = new SubjectInfo()
			{
				Address = new SubjectAddress()
				{
					City = "city",
					CityPart = "CityPart",
					Country = "Country",
					CountryCode = "CountryCode",
					HomeNr1 = 12,
					HomeNr2 = "HomeNr2",
					Street = "Street",
					ZIP = "PSC"
				},
				AresAlias_KPP = "(null)",
				CompanyName = "CompanyName",
				DateCreated = new DateTime(2000, 1, 1),
				DateTerminated = new DateTime(3000, 1, 1),
				Dic = "dic",
				Ico = "ico",
				IsProblematicSubject = true,
				IsVatRegistered = false,
				LegalForm = new SubjectLegalForm()
				{
					Code = 1,
					Name = "NPF"
				},
				TradeRegister = "null"
			};
			Assert.IsTrue(a.AresAlias_OF == a.CompanyName);
			Assert.IsTrue(a.AresAlias_DV == a.DateCreated, "Date Created invalid! Please check ARES.");
			Assert.IsTrue(a.AresAlias_DZ == a.DateTerminated);
		}
		[TestMethod]
		public async Task FindTest()
		{
			var icos = new string[] { "44996306", "45534390", "63218551", "416 93 426", "48160849", "13702416", "75789612", "76017346", "15480429", "469 80 318", "40092283", "71009531", "411 90 360", "00435872", "11595205", "10596429", "47674971", "255 99 542", "72503696", "87303787", "42113555", "60016345", "16275616", "48183881", "74876732", "627 43 643", "75175614", "678 28 949", "60685247", "45465061", "43220096", "22624473", "11587326", "46985671", "11498293", "68463791", "45970521", "46504788", "63336049", "75022125", "74391241", "496 14 592", "17439221", "005 68 431", "45659265" };
			var requests = new List<AresRequest>();
			foreach (var ico in icos)
				requests.Add(new AresRequest() { FindIco = ico });
			var a = new AresService();
			var rarr = requests.ToArray();
			await a.FindAsync(rarr);
			foreach (var req in rarr)
			{
				var si = req.Succesfull ? req.Response : new SubjectInfo();
				Console.WriteLine(string.Format("{0}:{1}; [{2}], {3} - {4}", req.FindIco, req.Succesfull, si.CompanyName, si.DateCreated, si.DateTerminated));
			}
		}
	}
}
